package com.fancy.learncenter.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Animatable;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeControllerBuilder;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.image.ImageInfo;
import com.fancy.learncenter.adapter.base.CommonRecycleViewAdapter;
import com.fancy.learncenter.adapter.base.CustomViewHold;
import com.fancy.learncenter.bean.PicSavaDataBean;
import com.fancy.learncenter.utils.ToastUtil;
import com.fancy.learncenter.utils.Utils;
import com.superservice.lya.R;

import java.util.ArrayList;

import me.relex.photodraweeview.PhotoDraweeView;

/**
 * Created by hyy on 2018/5/28.
 */

public class PicDetailAdapter extends CommonRecycleViewAdapter<PicSavaDataBean> {
    int width;
    Activity mContext;

    public PicDetailAdapter(Activity mContext, ArrayList<PicSavaDataBean> itemDatas) {
        super(mContext, R.layout.activity_pic_detail_item, itemDatas);
        this.mContext = mContext;
        Resources resources = mContext.getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        width = dm.widthPixels;
    }

    boolean isSava = true;

    public PicDetailAdapter(Activity mContext, ArrayList<PicSavaDataBean> itemDatas, boolean isSava) {
        super(mContext, R.layout.activity_pic_detail_item, itemDatas);
        this.mContext = mContext;
        Resources resources = mContext.getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        width = dm.widthPixels;
        this.isSava = isSava;
    }

    @Override
    public void bindView(CustomViewHold customViewHold, final PicSavaDataBean item, final int position) {
        RelativeLayout rootView = customViewHold.getView(R.id.rootView);

        final PhotoDraweeView mPhotoDraweeView = customViewHold.getView(R.id.simpleDraweeView);

        PipelineDraweeControllerBuilder controller = Fresco.newDraweeControllerBuilder();
        controller.setUri(item.getImgUrl());//设置图片url
        controller.setOldController(mPhotoDraweeView.getController());
        controller.setControllerListener(new BaseControllerListener<ImageInfo>() {
            @Override
            public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) {
                super.onFinalImageSet(id, imageInfo, animatable);
                if (imageInfo == null || mPhotoDraweeView == null) {
                    return;
                }
                mPhotoDraweeView.update(imageInfo.getWidth(), imageInfo.getHeight());
            }
        });
        mPhotoDraweeView.setController(controller.build());

        TextView save_tv = customViewHold.getView(R.id.save_tv);

        if (isSava) {
            save_tv.setVisibility(View.VISIBLE);
        } else {
            save_tv.setVisibility(View.GONE);
        }

        if (item.isSava()) {
            save_tv.setText("已保存");
            save_tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ToastUtil.show("图片已保存至 /LinYa文件下");
                }
            });
        } else {
            save_tv.setText("保存");

            save_tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (savaCallBack != null) {
                        savaCallBack.saveCallBack(position);
                    }
                }
            });
        }


    }

    private SavaCallBack savaCallBack;

    public SavaCallBack getSavaCallBack() {
        return savaCallBack;
    }

    public void setSavaCallBack(SavaCallBack savaCallBack) {
        this.savaCallBack = savaCallBack;
    }

    public interface SavaCallBack {
        void saveCallBack(int position);
    }
}
